package com.base.caselib.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.caselib.R;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.commonlib.bean.HistoryText;
import com.base.commonlib.bean.RoadEntity;
import com.base.commonlib.utils.MineRoadUtil;
import com.base.commonlib.widget.SingleChooseDialog;

import java.util.List;

/**
 * 文本框
 * Created by weikailiang on 2019/10/12.
 */

public class EditTextCustom extends FrameLayout {
    private TextView mTvName;
    private EditText mEtText;
    private String mRowIndex;
    private ImageView mAddress;
    private int type = 0;//0 默认 1 地址 2 路段
    private Spinner mSpinner;
    private String fileSave;
    public EditTextCustom(@NonNull Context context,int address) {
        super(context);
        this.type = address;
        init(null);
    }

    public EditTextCustom(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public EditTextCustom(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    public void init(AttributeSet attrs){
        LayoutInflater.from(getContext()).inflate(R.layout.item_edtext, this);
        mTvName = findViewById(R.id.tv_name);
        mEtText = findViewById(R.id.tv_edit);
        mAddress = findViewById(R.id.select_address);
        mSpinner = findViewById(R.id.spnner);
        if (type == 1){
            mAddress.setVisibility(VISIBLE);
            mAddress.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    ARouter.getInstance().build("/map/mapoi").navigation();
                }
            });
        }else if (type == 2){
            mAddress.setImageResource(R.drawable.ic_pickoption);
            mAddress.setVisibility(VISIBLE);
            mAddress.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    new SingleChooseDialog<RoadEntity>(getContext(), "请选择路段", MineRoadUtil.getMineRoads(BaseApp.getUser().getUserId()), new SingleChooseDialog.OnItemChooseListener() {

                        @Override
                        public void onItemChoose(Object o) {
                            mEtText.setText(o.toString());
                        }
                    }).show();
                }
            });

        }

    }
    public void setOnSpiner(final String userId){

        mEtText.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    showPop(mEtText,BaseApp.gethistoryText(userId,10));
                } else {
                    // 此处为失去焦点时的处理内容
                }
            }
        });
    }
    public void setReadLine(boolean isRead){
        if (mEtText!=null){
            if (isRead){
                mEtText.setEnabled(false);
            }
        }
    }
    public void setmTvName(String name){
        if (mTvName != null){
            mTvName.setText(name);
        }
    }
    public void setmEtText(String etText){
        if (mEtText != null){
            mEtText.setText(etText);
        }
    }
    public void setmEtTextHint(String etText){
        if (mEtText != null){
            mEtText.setHint(etText);
        }
    }
    public String getmEtText(){
        if (mEtText != null){
            return mEtText.getText().toString();
        }else {
            return "";
        }
    }

    public String getFileSave() {
        return fileSave;
    }

    public void setFileSave(String fileSave) {
        this.fileSave = fileSave;
    }

    public String getmRowIndex() {
        return mRowIndex;
    }

    public void setmRowIndex(String mRowIndex) {
        this.mRowIndex = mRowIndex;
    }
    private BaseAdapter<HistoryText> mAdapter;
    private void showPop(View view, final List<HistoryText> list){
        // 用于PopupWindow的View
        View contentView=LayoutInflater.from(getContext()).inflate(R.layout.item_address, null);

        RecyclerView mRecycler = contentView.findViewById(R.id.recycler);

        // 创建PopupWindow对象，其中：
        // 第一个参数是用于PopupWindow中的View，第二个参数是PopupWindow的宽度，
        // 第三个参数是PopupWindow的高度，第四个参数指定PopupWindow能否获得焦点
        final PopupWindow window = new PopupWindow(contentView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        // 设置PopupWindow的背景
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 设置PopupWindow是否能响应外部点击事件
        window.setOutsideTouchable(true);
        // 设置PopupWindow是否能响应点击事件
        window.setTouchable(true);
        mAdapter = new BaseAdapter<HistoryText>(list, getContext()) {

            @Override
            public void convert(BaseHolder holder, HistoryText randomDetail, boolean isHeader, boolean isBottom) {
                TextView target = holder.getView(R.id.text1);
                final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) target.getLayoutParams();
                lp.width = BaseApp.SCREEN_WIDTH;
                lp.height=LayoutParams.WRAP_CONTENT;;//lp.height=LayoutParams.WRAP_CONTENT;
                target.setLayoutParams(lp);
                target.setText(randomDetail.getText());
            }
        };
        mAdapter.putNormalType(R.layout.spinner_checked_text);
        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                HistoryText randomDetail = list.get(dataPosition);
                mEtText.setText(randomDetail.getText());
                window.dismiss();
            }
        });
        mRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecycler.setAdapter(mAdapter);
        // 显示PopupWindow，其中：
        // 第一个参数是PopupWindow的锚点，第二和第三个参数分别是PopupWindow相对锚点的x、y偏移
        window.showAsDropDown(view);
        // 或者也可以调用此方法显示PopupWindow，其中：
        // 第一个参数是PopupWindow的父View，第二个参数是PopupWindow相对父View的位置，
        // 第三和第四个参数分别是PopupWindow相对父View的x、y偏移
        // window.showAtLocation(parent, gravity, x, y);
    }
}
